package com.hs.dnms.dwr;

import java.util.List;
import java.util.Map;

import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
import com.hs.core.dwr.DwrHelper;
import com.hs.core.utils.BigNumCal;

public class DataProduceHelper   extends DwrHelper{
	
public String getSeqValue(String seqname){
	String sql = "select to_char(" + seqname + ".nextval) as seqvalue from dual";
	List datalist = this.getDbo().queryList(sql, null);
	Map data = (Map)datalist.get(0);
	return (String)data.get("seqvalue");
}

/**
 * 数据加工提交审核
 * @param id
 * @return
 */
public String submitAudit(String id){ 
	String sql = "update DNMS_MAGAZINE set STATE=2 where id =?";
	 this.getDbo().update(sql, new Object[]{id});
	
	return "";
}
/**
 * 添加疾病信息
 * @param disname
 * @param ensidname
 * @param ispara
 * @param istangyao
 * @param iszhongyao
 * @param iswest
 * @param istuina
 * @param iszhenjiu
 * @param isshoushu
 * @param curemethod
 * @return
 */	
public String addDis(String fileid,String disname,String ensidname,String ispara,String istangyao,String iszhongyao,String iswest,String istuina,String iszhenjiu,String isshoushu,String curemethod){
 	String disid = getSeqValue("SEQ_DNMS_DISEASE_ID");
	String sql = "INSERT INTO DNMS_DISEASE (ID, DISEASENAME, DISEASENAMEN, ISPARA, ISTANGYAO, ISZHONGY, ISWEST, ISZHENJIU, ISTUINA, ISSHOUSHU, CUREMETHOD, INDEXID) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)"; 
	this.getDbo().update(sql, new Object[]{disid,disname,ensidname,ispara,istangyao,iszhongyao,iswest,iszhenjiu,istuina,isshoushu,curemethod,fileid});
	StringBuffer buffer = new StringBuffer();
	if("1".equals(istangyao)){
		buffer.append("汤药治疗,");
	}
	if("1".equals(iszhongyao)){
		buffer.append("中成药治疗,");
	}
	if("1".equals(iswest)){
		buffer.append("西医治疗,");
	}
	if("1".equals(istuina)){
		buffer.append("推拿治疗,");
	}
	if("1".equals(iszhenjiu)){
		buffer.append("针灸治疗,");
	}
	if("1".equals(isshoushu)){
		buffer.append("手术治疗,");
	}
	StringBuffer returnstr = new StringBuffer("[{");
	returnstr.append("id:'").append(disid).append("',");
	returnstr.append("disname:'").append(disname).append("',");
	returnstr.append("disnamen:'").append(ensidname).append("',"); 
	returnstr.append("content:'").append(buffer).append("'"); 
	returnstr.append("}]"); 
	return disid;
}
/**
 * 删除疾病信息
 * @param disid
 * @return
 */
public String delDis(String disid){
	String returnstr = "";
	String sql = "delete from DNMS_DISEASE where INDEXID =?";
	this.getDbo().update(sql, new Object[]{disid});
	
	return disid;
}
/**
 * 查询方剂所有中药信息
 * @param disid
 * @return
 */
public String queryHerbcout(String preid){ 
	String sql = "select to_char(count(*)) as countall from DNMS_HERB where preid=?";
	List datalist = this.getDbo().queryList(sql, new Object[]{preid});
	Map data = (Map)datalist.get(0);
	String count = (String)data.get("countall");
	if("0".equals(count))
		return preid;
	return "";
}
/**
 * 查询证候的所有方剂信息
 * @param disid
 * @return
 */
public String queryPrecout(String symid){ 
	String sql = "select to_char(count(*)) as countall from DNMS_PRE where symid=?";
	List datalist = this.getDbo().queryList(sql, new Object[]{symid});
	Map data = (Map)datalist.get(0);
	String count = (String)data.get("countall");
	if("0".equals(count))
		return symid;
	return "";
}
/**
 * 查询穴位组的所有穴位信息
 * @param disid
 * @return
 */
public String queryPointcout(String groupid){ 
	String sql = "select to_char(count(*)) as countall from DNMS_POINT where PGROUPID=?";
	List datalist = this.getDbo().queryList(sql, new Object[]{groupid});
	Map data = (Map)datalist.get(0);
	String count = (String)data.get("countall");
	if("0".equals(count))
		return groupid;
	return "";
}
/**
 * 更新疾病信息
 * @param disname
 * @param ensidname
 * @param ispara
 * @param istangyao
 * @param iszhongyao
 * @param iswest
 * @param istuina
 * @param iszhenjiu
 * @param isshoushu
 * @param curemethod
 * @return
 */
public String updateDis(String disid,String disname,String ensidname,String ispara,String istangyao,String iszhongyao,String iswest,String istuina,String iszhenjiu,String isshoushu,String curemethod){
	String returnstr = "";
	System.out.println(":::" + disid);
	String sql = "UPDATE DNMS_DISEASE SET DISEASENAME=?, DISEASENAMEN=?, ISPARA=?, ISTANGYAO=?, ISZHONGY=?, ISWEST=?, ISZHENJIU=?, ISTUINA=?, ISSHOUSHU=?, CUREMETHOD=? WHERE ID=? ";
	returnstr = this.getDbo().update(sql, new Object[]{disname,ensidname,ispara,istangyao,iszhongyao,iswest,iszhenjiu,istuina,isshoushu,curemethod,disid}) + "";
	
	return disid;
}

/**
 * 添加并发病信息
 * @param paradisname
 * @param memo
 * @param disid
 * @return
 */
public String addParaDis(String paradisname,String memo,String disid){
	String returnstr = "";
	String paraid = getSeqValue("SEQ_DNMS_DISEASE_ID");
	String sql = "insert into DNMS_PARADIS(id,PARADISNAME,DISMEMO,DISID) VALUES(?,?,?,?)";
	returnstr = this.getDbo().update(sql, new Object[]{paraid,paradisname,memo,disid})+"";
	return paraid+"$"+ paradisname;
}
/**
 * 删除疾病并发病信息
 * @param disid
 * @return
 */
public String delParaDis(String disparaid){
	String returnstr = "";
	String sql = "delete from  DNMS_PARADIS where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{disparaid})+"";
	
	return disparaid;
}
/**
 * 更新疾病并发症信息
 * @param disparaid
 * @param paraname
 * @param memo
 * @return
 */
public String updateParaDis(String disparaid,String paraname,String memo){
	String returnstr = "";
	String sql = "update DNMS_PARADIS set PARADISNAME=?,DISMEMO=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{paraname,memo,disparaid})+"";
	return returnstr;
}
/**
 * 添加证候信息
 * @param paradisname
 * @param memo
 * @param disid
 * @return
 */
public String addSym(String symname,String memo,String disid){
	String returnstr = "";
	String symid = this.getSeqValue("SEQ_DNMS_SYM_ID");
	String sql = "insert into DNMS_SYM(id,SYMPTOMNAME,BEIZHU,DISID) VALUES(?,?,?,?)";
	returnstr = this.getDbo().update(sql, new Object[]{symid,symname,memo,disid})+"";
	
	return symid + "$" + symname;
}
/**
 * 删除证候信息
 * @param disid
 * @return
 */
public String delSym(String symid){
	String returnstr = "";
	
	String sql = "delete from  DNMS_SYM where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{symid})+"";
	return symid;
}
/**
 * 更新疾病证候信息
 * @param disparaid
 * @param paraname
 * @param memo
 * @return
 */
public String updateSym(String symid,String symname,String memo){
	String returnstr = "";
	String sql = "update DNMS_SYM set SYMPTOMNAME=?,BEIZHU=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{symname,memo,symid})+"";
	
	return returnstr;
}
/**
 * 添加穴位组信息
 * @param pointgroupname
 * @param memo
 * @param disid
 * @return
 */
public String addPointGroup(String pointgroupname,String memo,String disid){
	String returnstr = "";
	String groupid = this.getSeqValue("SEQ_DNMS_POINTGROUP_ID");
	String sql = "insert into DNMS_POINTGROUP(id,PGROUPNAME,BEIZHU,SYMID) VALUES(?,?,?,?)";
	returnstr = this.getDbo().update(sql, new Object[]{groupid,pointgroupname,memo,disid})+""; 
	return groupid +"$" + pointgroupname;
}
/**
 * 查询证候方剂信息
 * @param symid
 * @return
 */
public String queryPreBySymid(String symid){
	StringBuffer buffer = new StringBuffer();
	String sql = "select * from dnms_pre where symid=?";
	List dataList = this.getDbo().queryList(sql, new Object[]{symid});
	for(int i=0;i<dataList.size();i++){
		Map data = (Map)dataList.get(i);
		buffer.append(data.get("ID")).append("$");
		buffer.append("[{");
		buffer.append("id:'").append(data.get("ID")).append("',");
		buffer.append("prename:'").append(data.get("PRENAME")).append("',");
		buffer.append("laiyf:'").append(data.get("FROMPRENAME")==null?"":data.get("FROMPRENAME")).append("',"); 
		
		buffer.append("jixing:'").append(DictCacheUtil.getInst().getCodeNameByCode(data.get("PRETYPE")+"", "9")).append("'"); 
		buffer.append("}]#");
	}
	return buffer.toString();
}
/**
 * 查询方剂中药信息
 * @param preid
 * @return
 */
public String queryHerbByPreid(String preid){
	StringBuffer buffer = new StringBuffer(); 
	String sql = "select * from dnms_herb where PREID=?";
	List dataList = this.getDbo().queryList(sql, new Object[]{preid});
	for(int i=0;i<dataList.size();i++){
		Map data = (Map)dataList.get(i);
		buffer.append(data.get("ID")).append("$");
		buffer.append("[{");
		buffer.append("id:'").append(data.get("ID")).append("',"); 
		buffer.append("herbname:'").append(data.get("HERBNAME")).append("',");
		buffer.append("jiliang:'").append(data.get("HERBCOUNT")).append("',"); 
		buffer.append("unit:'").append(DictCacheUtil.getInst().getCodeNameByCode(data.get("COUNTUNIT")+"", "11")).append("'"); 
		buffer.append("}]#");
	}
	return buffer.toString(); 
}
/**
 * 查询穴位组穴位信息
 * @param groupid
 * @return
 */
public String queryPointByGroupid(String groupid){
    StringBuffer buffer = new StringBuffer();
    String sql = "select * from DNMS_POINT where pgroupid=?";
	List dataList = this.getDbo().queryList(sql, new Object[]{groupid});
	for(int i=0;i<dataList.size();i++){
		Map data = (Map)dataList.get(i);
		buffer.append("<span id=point_").append(data.get("ID")).append("><font size='5'>").append(data.get("POINTNAME"))
		.append("</font><a href=javaScript:delPoint('").append(data.get("ID")).append("');> 删 除 </a></span>");
	} 
	return buffer.toString(); 
}
/**
 * 删除穴位组信息
 * @param pointgroupid
 * @return
 */
public String delPointGroup(String pointgroupid){
	String returnstr = "";
	String sql = "delete from  DNMS_POINTGROUP where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{pointgroupid})+"";
	
	return pointgroupid;
}
/**
 * 更新穴位组信息
 * @param disparaid
 * @param paraname
 * @param memo
 * @return
 */
public String updatePointgroup(String pgroupid,String groupname,String memo){
	String returnstr = "";
	String sql = "update DNMS_POINTGROUP set PGROUPNAME=?,BEIZHU=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{groupname,memo,pgroupid})+"";
	
	return returnstr;
}

/**
 * 添加穴位信息
 * @param pointgroupname
 * @param memo
 * @param disid
 * @return
 */
public String addPoint(String pointname,String memo,String pointgroupid){
	String returnstr = "";
	String pid = this.getSeqValue("SEQ_DNMS_POINT_ID");
	String sql = "insert into DNMS_POINT(id,POINTNAME,BEIZHU,PGROUPID) VALUES(?,?,?,?)";
	returnstr = this.getDbo().update(sql, new Object[]{pid,pointname,memo,pointgroupid})+""; 
	
	return pid+"$" + pointname;
}
/**
 * 删除穴位信息
 * @param pointid
 * @return
 */
public String delPoint(String pointid){
	String returnstr = "";
	String sql = "delete from  DNMS_POINT where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{pointid})+"";
	
	return pointid;
}
/**
 * 更新穴位信息
 * @param pointid
 * @param pointname
 * @param memo
 * @return
 */
public String updatePoint(String pointid,String pointname,String memo){
	String returnstr = "";
	String sql = "update DNMS_POINT set POINTNAME=?,BEIZHU=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{pointname,memo,pointid})+"";
	
	return returnstr;
}

/**
 * 添加方剂信息
 * @param prename
 * @param laiyjdf
 * @param jixing
 * @param memo
 * @param symid
 * @return
 */
public String addPre(String prename,String laiyjdf,String jixing,String memo,String symid){
 	String preid = this.getSeqValue("SEQ_DNMS_PRE_ID");
	String sql = "insert into DNMS_PRE(id,PRENAME,FROMPRENAME,PRETYPE,BEIZHU,SYMID) VALUES(?,?,?,?,?,?)";
	this.getDbo().update(sql, new Object[]{preid,prename,laiyjdf,jixing,memo,symid}); 
	String jixingstr = DictCacheUtil.getInst().getCodeNameByCode(jixing, "9");
	StringBuffer returnstr = new StringBuffer("[{");
	returnstr.append("id:'").append(preid).append("',");
	returnstr.append("prename:'").append(prename).append("',");
	returnstr.append("laiyf:'").append(laiyjdf).append("',"); 
	returnstr.append("jixing:'").append(jixingstr).append("'"); 
	returnstr.append("}]"); 
	return preid +"$" + returnstr;
}
/**
 * 方剂复制
 * @param preid
 * @return
 */
public String copyPreAndHerb(String preid,String mudisymid){
	StringBuffer returnstr = new StringBuffer("[{");
	String temp = "";
	String sql = "select * from DNMS_PRE WHERE ID = ?";
	List datalist = this.getDbo().queryList(sql, new Object[]{preid});
	if(datalist.size() > 0){
		Map data = (Map)datalist.get(0);
		String id =  data.get("ID")+"";

		String prename = (String)data.get("PRENAME");
		String laiyjdf = (String)data.get("FROMPRENAME");
		String pretype =  data.get("PRETYPE") +"";
		String beizhu = (String)data.get("BEIZHU");
		//String symid =  data.get("SYMID")+""; 
		
		String newid = this.getSeqValue("SEQ_DNMS_PRE_ID");
		String newsql = "insert into DNMS_PRE(id,PRENAME,FROMPRENAME,PRETYPE,BEIZHU,SYMID) VALUES(?,?,?,?,?,?)";
		this.getDbo().update(newsql, new Object[]{newid,prename,laiyjdf,pretype,beizhu,mudisymid}); 
		
		String jixingstr = DictCacheUtil.getInst().getCodeNameByCode(pretype, "9");
		
		returnstr.append("id:'").append(preid).append("',");
		returnstr.append("prename:'").append(prename).append("',");
		returnstr.append("laiyf:'").append(laiyjdf==null?"":laiyjdf).append("',"); 
		returnstr.append("jixing:'").append(jixingstr).append("'"); 
		returnstr.append("}]"); 
		temp =  newid + "$" + returnstr.toString();
		
		//复制中药
		DBMgr dbo = this.getDbo();
		List herblist = dbo.queryList("SELECT HERBNAME,HERBCOUNT,COUNTUNIT FROM DNMS_HERB WHERE PREID=?", new Object[]{id});
		String sqlherb = "insert into DNMS_HERB(id,HERBNAME,HERBCOUNT,COUNTUNIT,COUNTNUM,PREID) VALUES(SEQ_DNMS_HERB_ID.NEXTVAL,?,?,?,?,?)";
		
		for(int i =0;i<herblist.size();i++){
			Map dataherb = (Map)herblist.get(i);
			String hname = (String)dataherb.get("HERBNAME");
			String count = dataherb.get("HERBCOUNT")+"";
			String unit = dataherb.get("COUNTUNIT")+""; 
			String countnum = null;
		    if("2".equals(unit)&& null!=count){
		    	boolean isnum = BigNumCal.isNumeric(count);
		    	if(isnum){
		    		countnum = count;
		    	}
		    }
            dbo.update(sqlherb, new Object[]{hname,count,unit,countnum,newid});
		} 
	} 
	return temp;
}
/**
 * 删除方剂信息
 * @param pointid
 * @return
 */
public String delPre(String preid){
	String returnstr = "";
	String sql = "delete from  DNMS_PRE where id=?";
	String delherb = "delete from DNMS_HERB where preid=?";
	this.getDbo().update(sql, new Object[]{preid});
	this.getDbo().update(delherb, new Object[]{preid});
	
	return preid;
}
/**
 * 添加古书籍引用信息
 * @param bookname
 * @param content
 * @param memo
 * @param magaid
 * @return
 */
public String addBookinfo(String bookname,String content,String memo, String magaid){
 	String seqid = this.getSeqValue("SEQ_DNMS_OLDFILE_ID");
	String sql = "insert into DNMS_OLDFILE(id,BOOKNAME,CONTENT,MEMO, MAGAID) VALUES(?,?,?,?,?)";
	this.getDbo().update(sql, new Object[]{seqid,bookname,content,memo ,magaid}); 
 	StringBuffer returnstr = new StringBuffer("[{");
	returnstr.append("id:'").append(seqid).append("',");
	returnstr.append("bookname:'").append(bookname).append("',");
	returnstr.append("bookcontent:'").append(content).append("'");  
	returnstr.append("}]"); 
	return seqid +"$" + returnstr;
}
/**
 * 删除古书籍引用信息
 * @param pointid
 * @return
 */
public String delBookinfo(String bookid){
	String returnstr = "";
	String sql = "delete from  DNMS_OLDFILE where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{bookid})+"";
	
	return bookid;
}
/**
 * 更新方剂信息
 * @param preid
 * @param prename
 * @param laiyjdf
 * @param jixing
 * @param memo
 * @param symid
 * @return
 */
public String updatePre(String preid,String prename,String laiyjdf,String jixing,String memo){
	String returnstr = "";
	String sql = "update DNMS_PRE set PRENAME=?,FROMPRENAME=?,PRETYPE=?,BEIZHU=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{prename,laiyjdf,jixing,memo,preid})+""; 
	
	
	return returnstr;
}
/**
 * 添加中药信息
 * @param herbname
 * @param jiliang
 * @param jiliangdanwei
 * @param preid
 * @return
 */
public String addHerb(String herbname,String jiliang,String jiliangdanwei,String preid){
 	String herbid = this.getSeqValue("SEQ_DNMS_HERB_ID");
 	String countnum = null;
    if("2".equals(jiliangdanwei)&&null!=jiliang){
    	boolean isnum = BigNumCal.isNumeric(jiliang);
    	if(isnum){
    		countnum = jiliang;
    	}
    }
	String sql = "insert into DNMS_HERB(id,HERBNAME,HERBCOUNT,COUNTUNIT,COUNTNUM,PREID) VALUES(?,?,?,?,?,?)";
	this.getDbo().update(sql, new Object[]{herbid,herbname,jiliang,jiliangdanwei,countnum,preid}); 
	String unitstr = DictCacheUtil.getInst().getCodeNameByCode(jiliangdanwei, "11");

	StringBuffer returnstr = new StringBuffer("[{");
	returnstr.append("id:'").append(herbid).append("',");
	returnstr.append("herbname:'").append(herbname).append("',");
	returnstr.append("jiliang:'").append(jiliang).append("',"); 
	returnstr.append("unit:'").append(unitstr).append("'"); 
	returnstr.append("}]"); 
	return herbid +"$" + returnstr;
}
/**
 * 删除中药信息
 * @param pointid
 * @return
 */
public String delHerb(String herbid){
	String returnstr = "";
	String sql = "delete from  DNMS_HERB where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{herbid})+"";
	
	return herbid;
}
/**
 * 更新方剂信息
 * @param preid
 * @param prename
 * @param laiyjdf
 * @param jixing
 * @param memo
 * @param symid
 * @return
 */
public String updateHerb(String herbid,String herbname,String jiliang,String jiliangdw,String memo){
	String returnstr = "";
	String sql = "update DNMS_HERB set HERBNAME=?,HERBCOUNT=?,COUNTUNIT=? where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{herbname,jiliang,jiliangdw,herbid})+"";  
	return returnstr;
}
/**
 * 删除原文信息
 * @param pointid
 * @return
 */
public String deleteFile(String fileid){
	String returnstr = "";
	String sql = "delete from  DNMS_FILE where id=?";
	returnstr = this.getDbo().update(sql, new Object[]{fileid})+"";
	
	return fileid;
}
/**
 * 保存文献分类信息
 * @param pointid
 * @return
 */
public String saveDocInfo(String docid,String doctype,String doccatagory){
	String returnstr = "";
	DBMgr dbo = this.getDbo();
	String sql = "update DNMS_MAGAZINE set DOCUMENTTYPE=?,DOCCATAGORY=? where id=?";
	dbo.update(sql, new Object[]{doctype,doccatagory,docid});
	if(null != doctype){
		dbo.update("delete from DNMS_MAGATYPE where magaid=?", new Object[]{docid});
		String[] doctypestr = doctype.split(",");
		for(int i = 0;i<doctypestr.length;i++){
			String dictcode = doctypestr[i];
			 
			dbo.update("insert into DNMS_MAGATYPE (id,magaid,dictcode) values(SEQ_DNMS_MAGATYPE_ID.NEXTVAL,?,?)",new Object[]{docid,dictcode});
		}
	}
	if(null != doccatagory){
		dbo.update("delete from DNMS_MAGACATAGORY where magaid=?", new Object[]{docid});
		String[] doccatastr = doccatagory.split(",");
		for(int i = 0;i<doccatastr.length;i++){
			String dictcode = doccatastr[i]; 
			dbo.update("insert into DNMS_MAGACATAGORY (id,magaid,dictcode) values(SEQ_DNMS_MAGACATAGORY_ID.NEXTVAL,?,?)",new Object[]{docid,dictcode});
		}
	}
	return "";
}
}
